/**
 * Created by jiadx on 2016/3/18.
 */
'use strict';

angular.module('Query')
    .controller('SingleProductStreamController', ['$scope', '$http', '$q', 'PublicModule', 'PublicEnum',
        function($scope, $http, $q, PublicModule, PublicEnum) {
            $scope.data = {
                basicInfo: {},
                bizCatalog: [],
                bizRecords: {
                    record: []
                },

                icon_position: {}
            };

            $scope.unfold = function(index) {
                var h = 0;
                for (var i = 0; i < index; i++) {
                    h += (angular.element('.list-exp_' + i).outerHeight() - 34);
                }
                angular.element('.query-context').scrollTop(h);
            };


            function queryProduct(Barcode) {
                var defer = $q.defer();
                $http({
                    method: 'POST',
                    url: 'api/ProductFlow/getProductFlow',
                    data: {
                        Parameter: {
                            Barcode: Barcode
                        }
                    }
                }).success(function(result) {
                    defer.resolve(result);
                }).error(function(error) {
                    defer.reject(error);
                });

                return defer.promise;
            }

            $scope.search = function() {
                if ($scope.Module.BarcodeMaster.FocusedRecord) {
                    queryProduct($scope.Module.BarcodeMaster.FocusedRecord.Barcode).then(function(strr) {
                            angular.forEach(strr.Data.bizCatalog, function(value) {
                                value.record = strr.Data.bizRecords[value.UniqueId];
                            });

                            $scope.data = {
                                basicInfo: strr.Data.basicInfo,
                                bizCatalog: strr.Data.bizCatalog,
                                bizRecords: strr.Data.bizRecords
                            };
                            //测试数据
                            // for (var i = 0; i < 5; i++) {
                            //     $scope.data.bizCatalog[0].record.push({
                            //         CollectDateTime: "2016-03-29T03:21:32.847Z",
                            //         QCFlag: true,
                            //         TerminalName: "打磨处",
                            //         Users: [],
                            //         WorkName: "打磨",
                            //         WorkTime: "0.00",
                            //         QCStatus:1

                            //     });
                            // }
                        },
                        function() {

                        });
                }
            };

            PublicModule.initModule($scope, 'BarcodeMaster');
        }
    ])

.directive("myScroll", function($window) {
    return function(scope, element, attrs) {
        var scrollEl = angular.element('div[class=query-context]');
        scrollEl.scroll(onScroll);

        scope.$on('$destroy', function() {
            scrollEl.off('scroll', onScroll);
        });

        function onScroll() {
            for (var i = 0; i < scope.data.bizCatalog.length + 2; i++) {
                var show_top = angular.element('.list-exp_' + i).offset().top,
                    hide_top = 139 + i * 35;
                if (show_top <= hide_top) {
                    angular.element('.list-icon-fix_' + i).removeClass('list-icon-hide');
                    angular.element('.list-icon_' + i).addClass('list-icon-show list-icon-hide-cursor');

                    for (var j = 0; j < i; j++) {
                        angular.element('.list-icon_' + j).removeClass('list-icon-show');
                    }
                    angular.element('.exp_' + i).addClass('list-icon-hide');
                } else {
                    angular.element('.exp_' + i).removeClass('list-icon-hide');
                    angular.element('.list-icon_' + i).removeClass('list-icon-show list-icon-hide-cursor');
                    angular.element('.list-icon-fix_' + i).addClass('list-icon-hide');
                }
            }
        }

    };
});
